import React, { Component } from 'react'
import PropTypes from 'prop-types'
import "./index.css"


export default class Header extends Component {
  static propTypes = {
    addTodo: PropTypes.func.isRequired
  }

  keyHandler = (event)=>{
  const {key,target} = event
  if(key == 'Enter' && target.value.trim()){
    const todoObj = {
      name:target.value,
      id:Date.now(),
      done:false,
    }
    this.props.addTodo(todoObj)
    target.value = ''
  }
 }
  render(){
    return (
      <div className="todo-header">
        <input type="text" onKeyUp={this.keyHandler} placeholder="请输入你的任务名称，按回车键确认" />
      </div>
    )
  }
}
